home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12792 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  8.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: Tue, 02 Apr 96 19:37:05 GMT
  6. Organization: none
  7. Message-ID: <828473825snz@genesis.demon.co.uk>
  8. References: <30C40F77.53B5@swsbbs.com> <4fc157$jsf@goanna.cs.rmit.EDU.AU> <dewar.823793746@schonberg> <4fms62$c0p@goanna.cs.rmit.EDU.AU> <4ft1ruINN6dr@keats.ugrad.cs.ubc.ca> <4g9255$74s@goanna.cs.rmit.EDU.AU> <824909942snz@genesis.demon.co.uk> <4h6gbp$guh@goanna.cs.rmit.EDU.AU>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4h6gbp$guh@goanna.cs.rmit.EDU.AU>
  15.            ok@goanna.cs.rmit.EDU.AU "Richard A. O'Keefe" writes:
  16.  
  17. <Sorry, I put thin on the back burner for a while, I nearly lost it>
  18.  
  19. >I complain about abs(X) giving wrong answers.
  20. >
  21. >Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  22. >
  23. >>Checking against overflow is required whatever representation is used.
  24. >
  25. >In the case of unary minus and abs, this is not true.
  26. >Those are the functions I was discussing.
  27.  
  28. Any my point remains that if you are checking against overflow you're
  29. just moving the problem somewhere else, not eliminating it.
  30.  
  31. >>Once this is established using 2's complement is no worse than any other
  32. >>representation.  Did you see my earlier reply in the thread on this subject?
  33. >
  34. >Yes, but it was neither comfort nor assistance.
  35. >
  36. >>If nothing else it seems to me that 2's complement is a big win with
  37. >>regards to implementing multiple-precision arithmetic since lower order
  38. >>bits of the result of most operations don't depend on the signs of the
  39. >>operands or the sign of the result.
  40. >
  41. >I have implemented multiple-precision arithmetic, and I must say I found
  42. >2s-complement a confounded nuisance.
  43.  
  44. I have too and it made the operation trivial (I'm talking about assembler
  45. rather than C).
  46.  
  47. > What I wanted, and what I used,
  48. >was arithmetic on N-bit natural numbers with overflow.  In short, what
  49. >I wanted for multiple precision arithmetic was
  50. >        N-bit natural + N-bit natural -> N-bit natural + carry
  51. >        N-bit natural - N-bit natural -> N-bit natural + borrow
  52. >        N-bit natural x N-bit natural -> 2N-bit natural
  53. >        2N-bit natural (/,%) N-bit natural -> N-bit natural, N-bit natural
  54. >Interpreting any of these as signed gave rise to serious complications.
  55.  
  56. 2's complement systems provide this almost for nothing since for + and -
  57. at least the bit manipulations are the same as unsigned, you just test a
  58. different set of conditions or flags. The common 2's complement archetectures
  59. I've come across support the interpretation of an operation as either
  60. 2's complement or unsigned.
  61.  
  62. More importantly the final status of the magnitude bits after + or - is
  63. independent of the sign bits which is a big win for multiple precision
  64. arithmetic - it means you can perform the operation on the lowest order
  65. word and progress to the highest order word and when you get there you've
  66. finished. How would you do it using a sign-magnitude representation?
  67.  
  68. >Oh dear.  I'm beginning to sound like Hermann Rubin.
  69. >
  70. >>>The practical consequence of this is that responsibility for ensuring that
  71. >>>the input to unary minus or absolute value is in range is passed onto the
  72. >>>programmer.  It is that which I am complaining about.  It doesn't make my
  73. >>>life as a programmer one teeny tiny bit easier.
  74. >
  75. >>It doesn't make it any harder either - see my earlier post.
  76. >
  77. >I saw your earlier post all right, and I didn't agree with it.
  78. >Perhaps in theory you might be right.  But in PRACTICE, it isn't true.
  79. >I have, for example, seen Pascal compilers where addition, subtraction,
  80. >and multiplication were checked for overflow, but unary minus, absolute
  81. >value, and division, were NOT checked for overflow, because they "obviously"
  82. >couldn't.  But they can.
  83.  
  84. Whether the compiler checks for overflow or not on basic arithmetic
  85. operations has nothing to do with whether the system uses 2's complement or
  86. some other representation, it is just a design decision for the compiler.`
  87.  
  88. >(Against myself, I must admit that one Pascal compiler for SPARCs did check
  89. >the overflow flag after multiplication, but the implementor hadn't bothered
  90. >to check what the multiplication routine actually _did_:  Sun's code always
  91. >cleared the overflow bit whatever happened.  Ouch!)
  92. >
  93. >>>Modular arithmetic in Ada 95 *is* the ticket for portable hackery.
  94. >>>Unsigned arithmetic in C is indeed defined to be modulo 2**n for some
  95. >>>n, but the bounds on n are very very loose, and the price of portability
  96. >>>is much programmer-inserted masking.
  97. >
  98. >>Well, a small amount of masking.
  99. >
  100. >Every operation except comparison needs it.  That's not a small amount.
  101.  
  102. No, for most 2's complement/unsigned operations higher order bits don't
  103. affect lower order bits. That means that most masking on operations performed
  104. in registers can be eliminated and in many cases a mask is only needed before
  105. writing the result to memory.
  106.  
  107. >>True, modulo arithmetic isn't useful in contexts where it simply generates
  108. >>the wrong result (however it does make it easy to test for overflow after
  109. >>an unsigned operation).
  110. >
  111. >It depends on what you mean by easy.  There are machines where the unsigned
  112. >arithmetic operations don't set the carry/borrow/overflow/whatever bit.
  113.  
  114. >There are others where there are minor glitches, e.g. on the M88100,
  115. >        unsigned x, y;
  116. >        x += y <<without overflow check>>
  117. >        x += y <<with    overflow check>>
  118. >        x += 1 <<without overflow check>>
  119. >all take one instruction, but
  120. >        x += 1 <<with   overflow check>>
  121. >takes two instructions, and the integer multiply instruction delivers the
  122. >bottom 32 bits with no overflow indication available at all. 
  123.  
  124. I'm arguing about the merits of 2's complement in general, not wjether there
  125. are poor implementations of it. Implementations can be fixed and new
  126. archetecture designs can be done properly. in their way microprocessors
  127. like the 6502 got it right as indeed has the x86.
  128.  
  129. >I suspect that you mean that it is easy in C using unsigned arithmetic
  130. >to test for overflow by using a short sequence of instructions.  I am
  131. >well aware of that, and I am also aware that it isn't easy, although C++
  132. >or Ada could let you do this without cluttering up the visual appearance
  133. >of the code (user-overloaded inline operators).
  134.  
  135. Multiple-precision arithmetic isn't great, whatever the representation you
  136. use. In my view 2's complement is still the best bet though unless you
  137. can show me a very efficient method using some other representation.
  138.  
  139. >>Only if the code doesn't test for wrap to zero. Unsigned types make this
  140. >>test very easy.
  141. >
  142. >YES BUT THEY MAKE IT ******NECESSARY*******.
  143. >(and in C, they don't make it any easier than signed types, in practice).
  144. >
  145. >The whole point that I am making is that MY code gets cluttered up!
  146.  
  147. I agree but that's a different issue.
  148.  
  149. >>>Overflows aren't the problem.  Restricted machine arithmetic is the problem.
  150. >
  151. >>It depends on where you put the limit. Language support for multiple-precision
  152. >>arithmetic or specified precision beyond 64 bits (or possibly 128 bits)
  153. >>has some uses but they are rare across the whole spectrum of code.
  154. >
  155. >I haven't been asking for multiple precision arithmetic.
  156.  
  157. I made the point that multiple-precision arithmetic was a strong mark in
  158. 2's complement's favour - you disagreed.
  159.  
  160. >I haven't been asking for specified precision beyond 64 bits.
  161. >(In my computing youth, I had 78 bits + sign and was happy.  Sigh.)
  162. >All I keep on saying is that
  163. >    - hardware arithmetic which delivers "wrong" results
  164. >    - combined with compilers that don't check the warning flags
  165. >      that ARE available in many architectures
  166. >push a lot of implementation complexity into MY code that doesn't belong
  167. >there, and I don't like it.
  168.  
  169. There's a valid compiler/language issue there but it has little or nothing
  170. to do with 2's complement.
  171.  
  172. There do seem to be separate issues that are being confused here. In this
  173. and certainly previous postings you've indicated a string dislike for
  174. 2's complement but haven't shown any clear disadvantages to it (there is
  175. abs() but in practice I've never come across a situation where that was an
  176. issue). On the other hand I see that in nearly all cases that show a
  177. difference 2's complement has the advantage.
  178.  
  179. A separate issue is whether an archetecture, language or implementation has
  180. adequate support for boundary checking. It is certainly try that C's
  181. boundary checking leaves a lot to be desired (if it exists at all).
  182.  
  183. -- 
  184. -----------------------------------------
  185. Lawrence Kirby | fred@genesis.demon.co.uk
  186. Wilts, England | 70734.126@compuserve.com
  187. -----------------------------------------
  188.